Ext.define('PJ.view.registrationmanagement.ChooseUserEdit', {
    extend: 'AM.default.DefaultWindowForm',
    requires: ['AM.default.DateMonth'
               ],
    height: 430,
    width: 340,
    notEdit: true,
    grid: null,
    
    initComponent: function(){
    	var me = this;
    	var dept=$(document.getElementById("selectScope")).find("option:selected")[0];
    	var bt = '<span style="color:red;">*</span>';
    	if(me.flag==1){
    		this.items = [{
    			xtype: "container",
    			border: false,
    			layout: 'column',
        		defaults: {
                    xtype: 'textfield',
        			allowBlank: false,
        			labelWidth: 95,
        			labelAlign: 'right',
        			columnWidth: .98,
        			padding: '3 0 2 0'
        		},items: [{
        			xtype: 'textfield',
        			fieldLabel:bt + '单位',
        			name: 'deptName',
        			readOnly:true,
        			value: me.grid.recordName==null?dept.text:me.grid.recordName,
        			columnWidth: 0.89
        		},{
        			name: 'searchDepId',//树节点Id
                	value: me.grid.recordId==null?dept.value:me.grid.recordId,
        			hidden:true
        		},{
        			xtype: "button",
        			columnWidth:.09,
        			tooltip: '单位',
        			margin: "3 0 3 3",
        			editable: false,
        			iconCls:"configIco",
        			hidden:me.isReadOnly,
        			handler: function(button){
    					Ext.create("PJ.view.registrationmanagement.DeptTreeFrame", {
                            title: "请选择学员单位",
                            form:this
                        }).show();
        			}
        		},{
                	fieldLabel: bt + '姓名',
                    name: 'userName',
                    maxLength: 100
                }, {
                	xtype: 'combo',
                	fieldLabel: bt + '性别',
                    name: 'sexCode',
                    allowBlank: false,
                    queryMode: 'local',
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: 'ajax',
                            url: me.grid.store.sexUrl + '?typeCode=1220',
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    defaultActive: false,
                    emptyText: '请选择'
                },{
                	xtype: "container",
        			border: false,
        			layout: 'column',
            		defaults: {
                        xtype: 'combo',
            			allowBlank: false,
            			labelWidth: 95,
            			labelAlign: 'right'
            		},items: [{
            			fieldLabel: bt + '出生年月',
                        name: "birthdayYear",
                        queryMode: 'local',//'remote',
                        allowBlank: false,
                        store: Ext.create("Ext.data.ArrayStore", {
                            singleton: true,
                            fields: [{
                                name: 'value',
                                mapping: "Year",
                                type: 'string'
                            }, {
                                name: 'text',
                                mapping: "year",
                                type: 'string'
                            }],
                            proxy: {
                                type: 'ajax',
                                url: me.grid.store.yearUrl,
                                reader: Ext.create('Ext.data.reader.Json', {
                                    type: 'json',
                                    root: 'YEARS',
                                    successProperty: 'success'
                                })
                            },
                            autoLoad: true
                        }),
                        valueField: 'value',
                        displayField: 'text',
                        editable: false,
                        emptyText: '请选择',
            			columnWidth: .7
            		},{
            			fieldLabel: '',
                        name: "birthdayMouth",
                        queryMode: 'local',//'remote',
                        allowBlank: false,
                        padding: '0 0 0 3',
                        store: Ext.create("Ext.data.ArrayStore", {
                            singleton: true,
                            fields: [{
                                name: 'value',
                                mapping: "Month",
                                type: 'string'
                            }, {
                                name: 'text',
                                mapping: "month",
                                type: 'string'
                            }],
                            proxy: {
                                type: 'ajax',
                                url: me.grid.store.monthUrl,
                                reader: Ext.create('Ext.data.reader.Json', {
                                    type: 'json',
                                    root: 'MONTHS',
                                    successProperty: 'success'
                                })
                            },
                            autoLoad: true
                        }),
                        valueField: 'value',
                        displayField: 'text',
                        editable: false,
                        emptyText: '请选择',
            			columnWidth: .3
            		}]
                }, {
                    xtype: "combo",
                    fieldLabel: bt + '民族',
                    name: "folkCode",
                    queryMode: 'local',//'remote',
                    allowBlank: false,
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: 'ajax',
                            url: me.grid.store.folkCodeUrl + '?typeCode=1224',
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    emptyText: '请选择'
                },{
                	fieldLabel: '手机号',
                	name: 'mobileTelephone',
                	allowBlank: true,
                	/*vtype:'phoneRepeat',
                	phoneRepeat:{
                		classId:this.classId,
                		userId:this.userId,
                	},*/
                	minLength:11,
                	maxLength:11,
                	regex: /^[1-9]*[0-9][0-9]*$/,
                	listeners: {
                		blur:function(){
                			if(this.value.length==11){
                				var url=this.up("window").grid.store.checkPhoneRepeatUrl;
                				Ext.Ajax.request({
                					url: url,
                					async: true,
                					params: {
                						searchClassID: me.classId,
                						mobileTelephone:this.value,
                						searchNoUserIds:me.userId
                					},
                					success: function(response){
                						var data = response.responseText;
                						var json = Ext.decode(data, true);
                						if (json.state!=100) {
                							me.phoneFlag=true;
                							Orchid.alert('消息', '手机号码重复，请重新输入！');
                						}else{
                							me.phoneFlag=false;
                						}
                						return;
                					}
                				});
                			}
    					}
                	}
                },{
                	xtype: 'combo',
                	fieldLabel: bt + '职级',
                    name: 'rank',
                    allowBlank: false,
                    queryMode: 'local',
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: 'ajax',
                            url: me.grid.store.sexUrl + '?typeCode=1228&deptId='+me.grid.recordId+"&nolevelCode="+me.flag,
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    defaultActive: false,
                    emptyText: '请选择'
                }/*, {
                    xtype: 'datemonthfield',
                    fieldLabel: bt + '出生年月',
                    name: 'birthday',
                    allowBlank: false,
                    editable: false
                }*/, {
                	fieldLabel: bt + '单位及职务',
                	name: 'dutyRemark',
                    allowBlank: false,
                },{
                	xtype : 'displayfield',
                	value : '<div style="color: red;margin-left: 100px;letter-spacing: 2px;">格式：单位 + 部门 + 职务<br/>如：东城区景山街道工委书记<br/>&emsp;&emsp;市发展改革委研究室主任<br/>&emsp;&emsp;北京工业大学规划学院院长</div>'
                }, {
                	xtype: "textareafield",
                	name: 'remark',
                    allowBlank: true,
                    maxLength: 1500,
                    fieldLabel: '备注',
//                    value:me.grid.signUpState
                }]
        	}, {
                xtype: 'hidden',
                name: 'openState',//班级状态
            	value: me.openState
            }, {
                xtype: 'hidden',
                name: "searchClassID",//培训班Id
                value: me.classId
            }/*, {
            	xtype: 'hidden',
            	name: 'signUpState',//报名状态
            	value: me.grid.signUpState
            }*/];
    	}else{
//    		var ro=this.isJuji;
    		var ro=false;
    		var rank;
    		this.items = [{
    			xtype: "container",
    			border: false,
    			layout: 'column',
        		defaults: {
                    xtype: 'textfield',
        			allowBlank: false,
        			labelWidth: 95,
        			labelAlign: 'right',
        			columnWidth: .98,
        			padding: '3 0 2 0'
        		},items: [{
                	fieldLabel: bt + '姓名',
                	readOnly:true,
                    name: 'userName',
                    maxLength: 100
                }, {
                	xtype: 'combo',
                	fieldLabel: bt + '性别',
                    name: 'sexCode',
                    allowBlank: false,
                    queryMode: 'local',
                	readOnly:ro,
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: 'ajax',
                            url: me.grid.store.sexUrl + '?typeCode=1220',
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    defaultActive: false,
                    emptyText: '请选择'
                },{
                	xtype: "container",
        			border: false,
        			layout: 'column',
            		defaults: {
                        xtype: 'combo',
            			allowBlank: false,
            			labelWidth: 95,
                    	readOnly:ro,
            			labelAlign: 'right'
            		},items: [{
            			fieldLabel: bt + '出生年月',
                        name: "birthdayYear",
                        queryMode: 'local',//'remote',
                        allowBlank: false,
                        store: Ext.create("Ext.data.ArrayStore", {
                            singleton: true,
                            fields: [{
                                name: 'value',
                                mapping: "Year",
                                type: 'string'
                            }, {
                                name: 'text',
                                mapping: "year",
                                type: 'string'
                            }],
                            proxy: {
                                type: 'ajax',
                                url: me.grid.store.yearUrl,
                                reader: Ext.create('Ext.data.reader.Json', {
                                    type: 'json',
                                    root: 'YEARS',
                                    successProperty: 'success'
                                })
                            },
                            autoLoad: true
                        }),
                        valueField: 'value',
                        displayField: 'text',
                        editable: false,
                        emptyText: '请选择',
            			columnWidth: .7
            		},{
            			fieldLabel: '',
                        name: "birthdayMouth",
                        queryMode: 'local',//'remote',
                        allowBlank: false,
                        padding: '0 0 0 3',
                        store: Ext.create("Ext.data.ArrayStore", {
                            singleton: true,
                            fields: [{
                                name: 'value',
                                mapping: "Month",
                                type: 'string'
                            }, {
                                name: 'text',
                                mapping: "month",
                                type: 'string'
                            }],
                            proxy: {
                                type: 'ajax',
                                url: me.grid.store.monthUrl,
                                reader: Ext.create('Ext.data.reader.Json', {
                                    type: 'json',
                                    root: 'MONTHS',
                                    successProperty: 'success'
                                })
                            },
                            autoLoad: true
                        }),
                        valueField: 'value',
                        displayField: 'text',
                        editable: false,
                        emptyText: '请选择',
            			columnWidth: .3
            		}]
                }, {
                    xtype: "combo",
                    fieldLabel: bt + '民族',
                    name: "folkCode",
                    queryMode: 'local',//'remote',
                    allowBlank: false,
                	readOnly:ro,
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: 'ajax',
                            url: me.grid.store.folkCodeUrl + '?typeCode=1224',
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    emptyText: '请选择'
                },{
                	fieldLabel: '手机号',
                	name: 'mobileTelephone',
                	allowBlank: true,
                	/*vtype:'phoneRepeat',
                	phoneRepeat:{
                		classId:this.classId,
                		userId:this.userId,
                	},*/
                	minLength:11,
                	maxLength:11,
                	regex: /^[1-9]*[0-9][0-9]*$/,
                	listeners: {
                		blur:function(){
                			if(this.value.length==11){
                				var url=this.up("window").grid.store.checkPhoneRepeatUrl;
                				Ext.Ajax.request({
                					url: url,
                					async: true,
                					params: {
                						searchClassID: me.classId,
                						mobileTelephone:this.value,
                						searchNoUserIds:me.userId
                					},
                					success: function(response){
                						var data = response.responseText;
                						var json = Ext.decode(data, true);
                						if (json.state!=100) {
                							me.phoneFlag=true;
                							Orchid.alert('消息', '手机号码重复，请重新输入！');
                						}else{
                							me.phoneFlag=false;
                						}
                						return;
                					}
                				});
                			}
    					}
                	}
                },{
                	xtype: 'combo',
                	fieldLabel: bt + '职级',
                    name: 'rank',
                    allowBlank: false,
                    queryMode: 'local',
                	readOnly:ro,
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: 'ajax',
                            url: me.grid.store.sexUrl + '?typeCode=1228&deptId='+me.grid.recordId,
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    defaultActive: false,
                    emptyText: '请选择'
//                    ,listeners:{
//                    	change:function(combo){
//                        	if(this.rank!=undefined){
//                        		if(combo.getValue()==1380||combo.getValue()==1381){
//                        			combo.setValue(this.rank);
//                        			Orchid.alert('消息', '您无权限操作！');
//                        		}else
//                        			this.rank=combo.getValue();
//                        	}else
//                        		this.rank=combo.getValue();
//                    	} 
//                    }  
                }/*, {
                    xtype: 'datemonthfield',
                    fieldLabel: bt + '出生年月',
                    name: 'birthday',
                    allowBlank: false,
                    editable: false
                }*/, {
                	fieldLabel: bt + '单位及职务',
                	name: 'dutyRemark',
                	readOnly:ro,
                    allowBlank: false,
                },{
                	xtype : 'displayfield',
                	value : '<div style="color: red;margin-left: 100px;letter-spacing: 2px;">格式：单位 + 部门 + 职务<br/>如：东城区景山街道工委书记<br/>&emsp;&emsp;市发展改革委研究室主任<br/>&emsp;&emsp;北京工业大学规划学院院长</div>'
                }, {
                	xtype: "textareafield",
                	name: 'remark',
                    allowBlank: true,
                    maxLength: 1500,
                	readOnly:ro,
                    fieldLabel: '备注',
//                    value:me.grid.signUpState
                }]
        	}, {
                xtype: 'hidden',
                name: 'openState',//班级状态
            	value: me.openState
            }, {
                xtype: 'hidden',
                name: "searchClassID",//培训班Id
                value: me.classId
            }/*, {
            	xtype: 'hidden',
            	name: 'signUpState',//报名状态
            	value: me.grid.signUpState
            }*/];
    	}
        
        this.buttons = [{
            text: '确定',
            handler: function(button, event){
                var win = button.up('window');
                win.sub();
            }
        }, {
            text: '关闭',
            scope: this,
            handler: this.close
        }];
        
        this.callParent(arguments);
    },
    sub: function(){
        var win = this;
        var form = win.down('form');
        var baseForm = form.getForm();
        if (!baseForm.isValid()) {
            return;
        }
        if(win.phoneFlag){
        	Orchid.alert('消息', '手机号码重复，请重新输入！');
        	return;
        }
        var mobileTelephone=form.getForm().findField("mobileTelephone").getValue();
        if(mobileTelephone!=''&&mobileTelephone.length!=11){
        	Orchid.alert('消息', '手机号位数不正确！');
        	return;
        }
        if(win.flag == 2){
        	url = win.grid.store.addUrl;
        }else{
        	url = win.grid.store.modifyUrl;
        }
        win.submit({
            url:url,
            params: {
            	searchClassID: win.classId,
            	userId: win.userId,
            	signUpState:win.grid.signUpState
    		},
            success: function(f, action){
                if (action.result.success) {
                	Orchid.alert('消息', action.result.msg, function(){
                        var gridPanel = win.grid;
                        var store = gridPanel.getStore();
                        store.reload();
                        if(gridPanel.title!='已选列表'){
                        	store=win.grid.up().down('ChooseUserList').getStore()
                        	store.reload();
                        }
                        win.close();
                    });
                }
            },
            failure: function(f, action){
                if (action.result.msg) {
                    Orchid.alert('消息', action.result.msg);
                }
                else {
                	Orchid.alert('消息', '操作失败！');
                }
            }
        });
    }
});
